package com.example.dps;

import java.text.DecimalFormat;
import java.text.NumberFormat;
import java.util.ArrayList;

import android.app.Activity;
import android.app.AlertDialog;
import android.app.Dialog;
import android.content.ContentValues;
import android.content.Intent;
import android.database.Cursor;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.drawable.Drawable;
import android.net.Uri;
import android.os.Bundle;
import android.provider.MediaStore;
import android.util.Log;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.View.OnTouchListener;
import android.view.ViewGroup.LayoutParams;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.SeekBar;
import android.widget.TextView;
import android.widget.SeekBar.OnSeekBarChangeListener;
import com.downpayment.dps.R;




/**
 * 
 * @author merrickc
 * 
 * Activity to display savings amount and to set the savings goal for the user
 *
 */
public class SetSavingsGoalActivity extends Activity{

	DBHelper dbhelper;
	SeekBar downPaymentSlider;
	TextView downPaymentText;
	
	ActionBarHelper actionBarHelper;
	AccountDBHelper accountDBHelper;
	
	public ArrayList<Integer> scrollViewImageIDs = new ArrayList<Integer>();
	
	DecimalFormat formatter = new DecimalFormat("$#,###");
	private int pictureIndex;
	
	
	/**
	 * step 1 = choose house
	 * step 2 = choose down payment goal
	 */
	int stepNum = 1;
	
	/** number of steps for set savings activity **/
	int numSteps = 2;
	
	boolean accountCreated = false;
	

	@Override
	public void onDestroy(){
		super.onDestroy();
		
		this.dbhelper.close();
		
	}
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		
		setContentView(R.layout.set_savings_step_one_layout);
		
		actionBarHelper = new ActionBarHelper(this);
		
		addOnClickListeners();
		
		dbhelper = new DBHelper(this);
		accountDBHelper = new AccountDBHelper(this);

		
		initScrollView();
		
		
		
		
		
	}
	
	public void onResume(){
		super.onResume();
		
		
		if(this.accountDBHelper.accountTableEmpty())
		
		if(stepNum == 1) initScrollView();
		if(stepNum == 2) restoreDBValues();
	}
	
	
	@Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
		// TODO Auto-generated method stub
		super.onActivityResult(requestCode, resultCode, data);
		
		if (resultCode == RESULT_OK){
			
		    Uri targetUri = data.getData();
		    Bitmap bitmap;
		    String[] filePathColumn = {MediaStore.Images.Media.DATA};

			Cursor cursor = getContentResolver().query(
			                   targetUri, filePathColumn, null, null, null);
			cursor.moveToFirst();

			int columnIndex = cursor.getColumnIndex(filePathColumn[0]);
			String filePath = cursor.getString(columnIndex);
			cursor.close();


     
     
			//** set header image to this image **/
			SetSavingsGoalActivity.this.dbhelper.updateImage(filePath);
			
			SetSavingsGoalActivity.this.finish();
			Intent intent = new Intent(SetSavingsGoalActivity.this, TabRoot.class);
			intent.putExtra("START_TAB", "set_savings_goal");
			SetSavingsGoalActivity.this.startActivity(intent);
			
			
			
			
		}
		
	}
	
	@Override
	protected Dialog onCreateDialog(int id) {

		AlertDialog.Builder builder = null;
		LayoutInflater inflater = null;
		
		switch (id) {
		case Constants.SET_GOAL_INFO:
			builder = new AlertDialog.Builder(this);
		    // Get the layout inflater
		    inflater = this.getLayoutInflater();

		    // Inflate and set the layout for the dialog
		    // Pass null as the parent view because its going in the dialog layout
		    builder.setView(inflater.inflate(R.layout.set_goal_info_dialog_layout, null));
		    
		    builder.setNegativeButton("close", null);
		    
		    AlertDialog dialog = builder.create();
		    
		    
		    return dialog;

		}
		
		
		return null;
	}
    
	
	 private void restoreDBValues() {
			/** Restore values from DB **/
			int total = dbhelper.selectGoalTotal();
			downPaymentSlider = (SeekBar) findViewById(R.id.downPaymentSlider);
			downPaymentSlider.setProgress(total);
			
			downPaymentText = (TextView) findViewById(R.id.downPaymentText);
			downPaymentText.setText(formatter.format(total));
			
			int homePrice = dbhelper.selectHomePrice();
			
			SeekBar homePriceSlider = (SeekBar) findViewById(R.id.homePriceSlider);
			homePriceSlider.setProgress(homePrice);
			
			TextView homePriceText = (TextView) findViewById(R.id.homePriceText);
			homePriceText.setText(formatter.format(homePrice));
			
			
			
			SeekBar timeFrameSlider = (SeekBar) findViewById(R.id.timeFrameSlider);
			TextView timeFrameText = (TextView) findViewById(R.id.timeFrameText);
			timeFrameText.setText(timeFrameSlider.getProgress()-1 + " Months");
			
			SeekBar monthlyAmountSlider = (SeekBar) findViewById(R.id.monthlyAmountSlider);
			
			
			double tf = timeFrameSlider.getProgress()-1;
			double t = total;
			int tempProgress = (int) (t/tf);
			int progress =  (int) (t/tf)/25*25;
			
			if(progress < tempProgress) progress+= 25;
						
			monthlyAmountSlider.setProgress(0);
			monthlyAmountSlider.setProgress(progress);
			TextView monthlyAmountText = (TextView) findViewById(R.id.monthlyAmountText);
			monthlyAmountText.setText(formatter.format(monthlyAmountSlider.getProgress()));
			
			
			
		
	}

	 
	 
	 public void addOnClickListeners(){
		 
		 
		 /** if step 1 **/
		 if(stepNum == 1){
			 
			 
			 ImageView addPictureButton = (ImageView) findViewById(R.id.camera_icon);
			 addPictureButton.setOnClickListener(new OnClickListener(){

				@Override
				public void onClick(View arg0) {
					// TODO Auto-generated method stub
					Intent intent = new Intent(Intent.ACTION_PICK,
					        android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
					      startActivityForResult(intent, 0);
				}
				 
			 });
			 
			 addPictureButton.setOnTouchListener(new OnTouchListener(){

					@Override
					public boolean onTouch(View v, MotionEvent event) {
						// TODO Auto-generated method stub
						
						
						ImageView view = (ImageView) v;
						
						if(event.getAction() == MotionEvent.ACTION_DOWN){
							view.setImageDrawable(view.getResources().getDrawable(R.drawable.camera_icon_clicked));
						}
						else if (event.getAction() == MotionEvent.ACTION_UP){
							view.setImageDrawable(view.getResources().getDrawable(R.drawable.camera_icon));
						}
						else if(event.getAction() == MotionEvent.ACTION_CANCEL){
							view.setImageDrawable(view.getResources().getDrawable(R.drawable.camera_icon));
						}
						
						return false;
					}
					 
				 });
			 
			 
			 
			 ImageButton continueButton = (ImageButton)findViewById(R.id.setSavingsContinueButton);
			 
			 continueButton.setOnClickListener(new OnClickListener(){

				@Override
				public void onClick(View arg0) {
					
					SetSavingsGoalActivity.this.stepNum++;
					
					changeLayoutToCurrentStep();
					
				}
				 
			 });
			 
			 continueButton.setOnTouchListener(new OnTouchListener(){
					@Override
					public boolean onTouch(View v, MotionEvent event) {
						/** get the root element of the button view **/
				    	ImageView view = (ImageView) v;
				    	
				        if(event.getAction() == MotionEvent.ACTION_DOWN) {
				        	/** change the image button picture when clicked **/
				        	view.setImageDrawable(v.getResources().getDrawable(R.drawable.continue_btn_clicked));
				        } else if (event.getAction() == MotionEvent.ACTION_UP) {
				        	view.setImageDrawable(v.getResources().getDrawable(R.drawable.continue_btn));
				        }
				        else if (event.getAction() == MotionEvent.ACTION_CANCEL) {
				        	view.setImageDrawable(v.getResources().getDrawable(R.drawable.continue_btn));
				        }
						return false;
					}
				});

		 }
		 /** End Step 1 button listeners **/
		 
		 /** if stepNum = 2 **/
		 else if(stepNum == 2){
			 
			 ImageButton startSavingButton = (ImageButton) findViewById(R.id.startSavingButton);
			 ImageButton viewSavingsButton = (ImageButton) findViewById(R.id.viewSavingsButton);
			 
			 if(this.accountCreated){
				 viewSavingsButton.setVisibility(View.VISIBLE);
				 startSavingButton.setVisibility(View.GONE);
				 
				 viewSavingsButton.setOnClickListener(new OnClickListener(){

					@Override
					public void onClick(View arg0) {
						Intent intent = new Intent(SetSavingsGoalActivity.this, TabRoot.class);
						intent.putExtra("START_TAB", "view_savings");
						SetSavingsGoalActivity.this.startActivity(intent);
						
					}
					 
				 });
				 
				 viewSavingsButton.setOnTouchListener(new OnTouchListener(){
						@Override
						public boolean onTouch(View v, MotionEvent event) {
							/** get the root element of the button view **/
					    	ImageView view = (ImageView) v;
					    	
					        if(event.getAction() == MotionEvent.ACTION_DOWN) {
					        	/** change the image button picture when clicked **/
					        	view.setImageDrawable(v.getResources().getDrawable(R.drawable.continue_btn_clicked));
					        } else if (event.getAction() == MotionEvent.ACTION_UP) {
					        	view.setImageDrawable(v.getResources().getDrawable(R.drawable.continue_btn));
					        }
					        else if (event.getAction() == MotionEvent.ACTION_CANCEL) {
					        	view.setImageDrawable(v.getResources().getDrawable(R.drawable.continue_btn));
					        }
							return false;
						}
					});
				 
				 
			 }
			 else{
				 
				 viewSavingsButton.setVisibility(View.GONE);
				 startSavingButton.setVisibility(View.VISIBLE);
				 
				 startSavingButton.setOnClickListener(new OnClickListener(){

					 @Override
					 public void onClick(View view) {
						 Intent intent = new Intent(SetSavingsGoalActivity.this, TabRoot.class);
							intent.putExtra("START_TAB", "add_money");
							SetSavingsGoalActivity.this.startActivity(intent);
					 }
						 
				 });
				 
				 startSavingButton.setOnTouchListener(new OnTouchListener(){
						@Override
						public boolean onTouch(View v, MotionEvent event) {
							/** get the root element of the button view **/
					    	ImageView view = (ImageView) v;
					    	
					        if(event.getAction() == MotionEvent.ACTION_DOWN) {
					        	/** change the image button picture when clicked **/
					        	view.setImageDrawable(v.getResources().getDrawable(R.drawable.ready_start_saving_btn_clicked));
					        } else if (event.getAction() == MotionEvent.ACTION_UP) {
					        	view.setImageDrawable(v.getResources().getDrawable(R.drawable.ready_start_saving_btn));
					        }
					        else if (event.getAction() == MotionEvent.ACTION_CANCEL) {
					        	view.setImageDrawable(v.getResources().getDrawable(R.drawable.ready_start_saving_btn));
					        }
							return false;
						}
					});
				 
				 ImageView questionIcon = (ImageView) findViewById(R.id.question_icon);
				 questionIcon.setOnClickListener(new OnClickListener(){

					@Override
					public void onClick(View arg0) {
						
						showDialog(Constants.SET_GOAL_INFO);
						
					}
					 
				 });
				 
				 questionIcon.setOnTouchListener(new OnTouchListener(){

					@Override
					public boolean onTouch(View view, MotionEvent event) {
						ImageView v = (ImageView) view;
						
						if(event.getAction() == MotionEvent.ACTION_DOWN){
							v.setImageDrawable(v.getResources().getDrawable(R.drawable.tool_tip_icon_clicked));
						}
						else if (event.getAction() == MotionEvent.ACTION_UP || event.getAction() == MotionEvent.ACTION_CANCEL){
							v.setImageDrawable(v.getResources().getDrawable(R.drawable.tool_tip_icon));
						}
						
						
						return false;
					}
					 
				 });
				 
				 
				 LinearLayout helpfulTips = (LinearLayout) findViewById(R.id.helpfulTipsText);
				 helpfulTips.setOnClickListener(new OnClickListener(){

					@Override
					public void onClick(View arg0) {
						
						showDialog(Constants.SET_GOAL_INFO);
						
					}
					 
				 });
				 
				 
			 }
			 
			 
			 
		 }
		 
		 
	 }
	
	 
	/**
	 * adds the listeners and functionality to the seek bars
	 */
	public void addSeekBarListeners(){
		
		/*****************************************************/
		/** Handle the Home Price Seek Bar               *****/
		/*****************************************************/
		SeekBar homePriceSlider = (SeekBar) findViewById(R.id.homePriceSlider);
		homePriceSlider.setMax(600000);
		homePriceSlider.incrementProgressBy(10000);
		
		TextView homePriceText = (TextView) findViewById(R.id.homePriceText);
		
		homePriceSlider.setOnSeekBarChangeListener(new OnSeekBarChangeListener(){

			@Override
			public void onProgressChanged(SeekBar sb, int progress, boolean fromUser) {
				
				/** update home price value **/
				int homePrice = progress/10000;
				homePrice = homePrice*10000;

				/** update the associated text values **/
				TextView homePriceSliderValue = (TextView) findViewById(R.id.homePriceText);
				
			    NumberFormat formatter = new DecimalFormat("$###,###");
				
			    
			    /** TODO BUG AT THIS SET TEXT **/
				homePriceSliderValue.setText(formatter.format(homePrice));
				
				
				/**update db with new homePrice **/
				SetSavingsGoalActivity.this.dbhelper.updateHomePrice(homePrice);
				
				/*************************************************************************** 
				 * update Down Payment Percentage
				 */
				
				TextView downPaymentValue = (TextView) findViewById(R.id.downPaymentText);
				int downPaymentInt = Integer.parseInt(downPaymentValue.getText().toString().replace("$","").replace(",", ""));
				
				/** double values of integers **/
				double dDownPayment = (double) downPaymentInt;
				double dHomePrice = (double) homePrice;
				double downPaymentPercent = -1;
				
				/** text view containing DP percent **/
				TextView downPaymentPercentText = (TextView) findViewById(R.id.downPaymentPercentageText);
				
				if(dHomePrice != 0){
					downPaymentPercent = dDownPayment/dHomePrice*100;
				}
				
				/** if a proper percentage, display it **/
				if(downPaymentPercent >= 0 && dHomePrice > dDownPayment){

					NumberFormat percentFormatter = new DecimalFormat("(#.00%)");
					
					downPaymentPercentText.setText(percentFormatter.format(downPaymentPercent/100)); // I dont know why, but I had to divide the percent by 100 again
					
					downPaymentPercentText.setVisibility(View.VISIBLE);
					
				}
				/** else do not display the percentage **/
				else{
					downPaymentPercentText.setVisibility(View.GONE);
				}
				
				/** End updating down payment percent
				/***************************************************************************/
	
			}

			@Override
			public void onStartTrackingTouch(SeekBar arg0) {
				
			}

			@Override
			public void onStopTrackingTouch(SeekBar arg0) {
				
				
			}
			
		});
		
		
		

		/*****************************************************/
		/** Handle the Savings Goal Seek Bar             *****/
		/*****************************************************/
		downPaymentSlider = (SeekBar) findViewById(R.id.downPaymentSlider);
		downPaymentSlider.setMax(80000);
		downPaymentSlider.incrementProgressBy(500);
		
		downPaymentText = (TextView) findViewById(R.id.downPaymentText);
		
		downPaymentSlider.setOnSeekBarChangeListener(new OnSeekBarChangeListener(){

			@Override
			public void onProgressChanged(SeekBar slider, int progress, boolean fromUser) {
				TextView downPaymentText = (TextView) findViewById(R.id.downPaymentText);
				
				/** progress should only be multiples of 500 **/
				progress = progress/500;
				progress = progress*500;
				int downPaymentGoal = progress;
				
				downPaymentText.setText(formatter.format(downPaymentGoal));
				

				/**********************************************************************/
				/** update the Down Payment Percent 								 **/
				
				TextView homePriceValue = (TextView) findViewById(R.id.homePriceText);
				/** get integer value and strip $ and ,'s **/
				int homePriceInt = Integer.parseInt(homePriceValue.getText().toString().replace("$", "").replace(",", ""));
				
				/** double values of integers **/
				double dDownPayment = (double) downPaymentGoal;
				double dHomePrice = (double) homePriceInt;
				double downPaymentPercent = -1;
				
				/** text view containing DP percent **/
				TextView downPaymentPercentText = (TextView) findViewById(R.id.downPaymentPercentageText);
				
				if(dHomePrice != 0){
					downPaymentPercent = dDownPayment/dHomePrice*100;
				}
				
				/** if a proper percentage, display it **/
				if(downPaymentPercent > 0 && dHomePrice > dDownPayment){

					NumberFormat percentFormatter = new DecimalFormat("(#.0%)");
					
					downPaymentPercentText.setText(percentFormatter.format(downPaymentPercent/100)); // I dont know why, but I had to divide the percent by 100 again
					
					downPaymentPercentText.setVisibility(View.VISIBLE);
					
				}
				/** else do not display the percentage **/
				else{
					downPaymentPercentText.setVisibility(View.GONE);
				}

				/** END update Down Payment Percent      							 **/
				/**********************************************************************/
				
				/** update monthly max to = the Down Payment Goal **/
				SeekBar monthlyAmountSlider = (SeekBar) findViewById(R.id.monthlyAmountSlider);
				monthlyAmountSlider.setMax(downPaymentGoal/2);
				monthlyAmountSlider.setProgress(monthlyAmountSlider.getProgress());
				
				
				
				if(fromUser){
					
					
					/** update the time frame slider **/
					SeekBar timeFrameSlider = (SeekBar) findViewById(R.id.timeFrameSlider);

					TextView monthlyAmountText = (TextView) findViewById(R.id.monthlyAmountText);
					
					int timeFrameProgress = timeFrameSlider.getProgress();
					if(timeFrameProgress == 0) timeFrameProgress = 1;
					
					monthlyAmountSlider.setProgress(progress/timeFrameProgress);
					
					monthlyAmountText.setText(formatter.format(monthlyAmountSlider.getProgress()));
					
				}

				
			}

			@Override
			public void onStartTrackingTouch(SeekBar arg0) {
			}

			
			@Override
			public void onStopTrackingTouch(SeekBar seekBar) {

				
				TextView downPaymentText = (TextView) SetSavingsGoalActivity.this.findViewById(R.id.downPaymentText);
				
				int downPaymentValue = Integer.parseInt(downPaymentText.getText().toString().replace("$", "").replace(",", ""));
				
				/** if goal table is empty, insert new entry **/
				if(dbhelper.isTableEmpty()){
					
					ContentValues values = new ContentValues();
					values.put(DBHelper.COLUMN_TOTAL, downPaymentValue);
					values.put(DBHelper.COLUMN_SAVED, 0);
					
					dbhelper.insertGoalEntry(values);
					
				}
				/** else update the goal entry **/
				else{
					ContentValues values = new ContentValues();
					values.put(DBHelper.COLUMN_TOTAL, downPaymentValue);
					dbhelper.updateGoalEntry(values);
				}
			}
			
		});
		
		
		/*****************************************************/
		/** handle the time frame seekBar                *****/
		/*****************************************************/
		SeekBar timeFrameSlider = (SeekBar) findViewById(R.id.timeFrameSlider);
		timeFrameSlider.setMax(36);
		timeFrameSlider.setProgress(18);
		//timeFrameSlider.incrementProgressBy(500);

		timeFrameSlider.setOnSeekBarChangeListener(new OnSeekBarChangeListener(){

			@Override
			public void onProgressChanged(SeekBar seekBar, int progress,
					boolean fromUser) {
				// TODO Auto-generated method stub
				
				TextView timeFrameText = (TextView) findViewById(R.id.timeFrameText);
				timeFrameText.setText(progress+" Months");
				
				TextView monthlyAmountText = (TextView) findViewById(R.id.monthlyAmountText);
				
				if(progress == 0) progress = 1;
				
				/** update monthly amount **/
				if(fromUser){
					
					SeekBar downPaymentSlider = (SeekBar) findViewById(R.id.downPaymentSlider);
					SeekBar monthlyAmountSlider = (SeekBar) findViewById(R.id.monthlyAmountSlider);
					
					int total = downPaymentSlider.getProgress()/500*500;
					int timeFrame = progress;

					
					if(timeFrame == 0 )timeFrame = 1;
					
					double dt = total;
					double dtf = timeFrame;
					double dMonthAmount = dt/dtf;
					
					//Log.e("dt/dtf" , " dt/dtf = " + dMonthAmount);
					
					
					monthlyAmountSlider.setProgress((int) dMonthAmount);
					monthlyAmountText.setText(formatter .format(dMonthAmount));
				}
				else{
	
				}
				

				
			}
		
			@Override
			public void onStartTrackingTouch(SeekBar seekBar) {
				// TODO Auto-generated method stub
				
			}

			@Override
			public void onStopTrackingTouch(SeekBar seekBar) {
				// TODO Auto-generated method stub
				
			}
			
		});
		
		/*****************************************************/
		/** handle the monthly amount seek bar           *****/
		/*****************************************************/
		SeekBar monthlyAmountSlider = (SeekBar) findViewById(R.id.monthlyAmountSlider);
		monthlyAmountSlider.setMax(this.downPaymentSlider.getProgress()/2);
		
		/** set progress to DP / time Frame **/
		double dpAmount = downPaymentSlider.getProgress();
		double timeFrame = timeFrameSlider.getProgress();
		double monthlyAmount = dpAmount/timeFrame;
		monthlyAmountSlider.setProgress(0);
		monthlyAmountSlider.setProgress((int) monthlyAmount);
		
		monthlyAmountSlider.incrementProgressBy(25);

		monthlyAmountSlider.setOnSeekBarChangeListener(new OnSeekBarChangeListener(){

			@Override
			public void onProgressChanged(SeekBar seekBar, int progress,
					boolean fromUser) {
				// TODO Auto-generated method stub
				
				TextView monthlyAmountText = (TextView) findViewById(R.id.monthlyAmountText);
				
				
				
				if(fromUser){
					
					SeekBar downPaymentSlider = (SeekBar)findViewById(R.id.downPaymentSlider);
					int total = downPaymentSlider.getProgress()/500*500;
					
					SeekBar timeFrameSlider = (SeekBar) findViewById(R.id.timeFrameSlider);
					SeekBar monthlyAmountSlider = (SeekBar) findViewById(R.id.monthlyAmountSlider);
					
					int[] intervals = new int[timeFrameSlider.getMax()+1];
					
					double minDif = 1000000;
					int currentMinInterval = 0;
					int currentTimeFrame = 0;
					
					intervals[0] = 0;
					for(int i = 1; i < intervals.length; i++){
						
						int divisor = timeFrameSlider.getMax()-i;
						if(divisor == 0) divisor = 1;
						
						
						intervals[i] = total/(divisor);
						
						if(Math.abs(intervals[i] - progress) < minDif){
							currentMinInterval = i;
							currentTimeFrame = 36-i;
							minDif = Math.abs(intervals[i] - progress);

							monthlyAmountSlider.setProgress(intervals[currentMinInterval]);
							monthlyAmountText.setText(formatter.format(intervals[currentMinInterval]));
						}
						
					}
					
					
					
					timeFrameSlider.setProgress(currentTimeFrame);
					
				}

				



			}
		
			@Override
			public void onStartTrackingTouch(SeekBar seekBar) {
				
			}

			@Override
			public void onStopTrackingTouch(SeekBar seekBar) {
				
			}
			
		});
		
	}
	
	
	
	public void initScrollView(){
		
		
		/** populate images for scroll view **/
		getScrollViewImages();
		
		/** layout containing sv images **/
		LinearLayout imagesLayout = (LinearLayout) findViewById(R.id.inspirationImagesLayout);
		
		imagesLayout.removeAllViews();
		
		Drawable imageCustom = this.dbhelper.getImage();
		
		if(imageCustom != null && this.dbhelper.hasCustomImage()){
			
			/** add the custom image if it exists **/
			LinearLayout customImageContainer = new LinearLayout(getApplicationContext());
			customImageContainer.setLayoutParams(new LayoutParams(150,150));
			customImageContainer.setGravity(Gravity.CENTER);
			
			ImageView customImage = new ImageView(getApplicationContext());
			
			customImage.setImageDrawable(imageCustom);
			
//			customImage.setLayoutParams(new LayoutParams(90,90));
			customImage.setScaleType(ImageView.ScaleType.FIT_CENTER);
			
			customImageContainer.addView(customImage);
			
			imagesLayout.addView(customImageContainer);
			
		}

		
		/** index of this image in scroll view **/
		this.pictureIndex = 0;
		
		/** for each bitmap image, add image layout to scroll view **/
		for(final Integer id : scrollViewImageIDs){

			LinearLayout imageContainer = new LinearLayout(getApplicationContext());
			imageContainer.setLayoutParams(new LayoutParams(150,150));
			imageContainer.setGravity(Gravity.CENTER);
			
			ImageView image = new ImageView(getApplicationContext());
			image.setLayoutParams(new LayoutParams(140,140));
			image.setScaleType(ImageView.ScaleType.FIT_CENTER);
			
			image.setImageDrawable(getResources().getDrawable(id));
			
			imageContainer.addView(image);
			
			imagesLayout.addView(imageContainer);
			
			
			/** add click listener to change picture image **/
			
			imageContainer.setClickable(true);
			imageContainer.setOnClickListener(new OnClickListener(){

				@Override
				public void onClick(View view) {
					SetSavingsGoalActivity.this.dbhelper.updateImage(id + "");
					
					
					SetSavingsGoalActivity.this.finish();
					Intent intent = new Intent(SetSavingsGoalActivity.this, TabRoot.class);
					intent.putExtra("START_TAB", "set_savings_goal");
					SetSavingsGoalActivity.this.startActivity(intent);
					
				}
				
			});

			
			
		}
		
	}
	
	public void getScrollViewImages(){
		
		scrollViewImageIDs = new ArrayList<Integer>();
		
		
		
		scrollViewImageIDs.add(R.drawable.house_1);
		
		scrollViewImageIDs.add(R.drawable.house_2);
		
		scrollViewImageIDs.add(R.drawable.house_3);
		
		scrollViewImageIDs.add(R.drawable.house_4);
		
		scrollViewImageIDs.add(R.drawable.house_5);
		
		scrollViewImageIDs.add(R.drawable.house_6);
		
	}
	
	
	/** changes the layout to the current step **/
	public void changeLayoutToCurrentStep(){
		
		if(stepNum == 1){
			
		}
		else if(stepNum == 2){
			this.setContentView(R.layout.set_savings_step_two);
			addSeekBarListeners();
			addOnClickListeners();
			restoreDBValues();
		}
		
		
		
	}
	
	
}
